package src;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.ViewPart;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MappingView extends ViewPart {
	private Table content;
	private TableViewer view;

	public void populate(File xmlFile, Table t) {
		try {
			DocumentBuilderFactory parseFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder parser = parseFactory.newDocumentBuilder();
			Document xmldoc = parser.parse(xmlFile);
			NodeList maps = xmldoc.getElementsByTagName("Mapping");
			for (int i = 0; i < maps.getLength(); i++) {
				Node phpatt = maps.item(i).getFirstChild();
				TableItem ti = new TableItem(t, 0);
				ti.setText(0, phpatt.getAttributes().getNamedItem("FilePath").getNodeValue());
				ti.setText(1, phpatt.getAttributes().getNamedItem("Position").getNodeValue());
				ti.setText(2,maps.item(i).getAttributes().getNamedItem("Position").getNodeValue());
			}

		} catch (Exception e) {
			System.out
					.println("An Error occured while parsing the XMLMapping document:");
			e.printStackTrace();
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		view = new TableViewer(parent);
		content = view.getTable();
		TableItem item = new TableItem(content, SWT.NULL);
		item.setText("No mapping or corresponding html file selected");
		//subscribe to the various listeners required.
		getSite().getPage().addPartListener(new IPartListener()
		{

			@Override
			public void partActivated(IWorkbenchPart part) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void partBroughtToTop(IWorkbenchPart part) 
			{
				//this is where we want to do most of the work for the demo.
				//when a part is brought to the top, we populate the mapping if it is a valid file.
				EditorPart e = (EditorPart)part.getAdapter(EditorPart.class);
				if(e!=null)
					{
						IFile f = (IFile) e.getEditorInput().getAdapter(IFile.class);
						if(f!=null)
						{
							File source = f.getFullPath().toFile();
							int tobereplaced = source.getAbsolutePath().indexOf("client_source.html");
							String path = source.getAbsolutePath().substring(0, tobereplaced);
							path= path +"client_mapping.xml";
							File mapping = new File(path);
							populate(mapping , content);
						}
					}
				//first, check which part was brought to the top.
				//if it is a valid .html file, we want to check for a corresponding mapping.
				if(part.getTitle().equals("client_source.html"))
				{
					//a file matching the specification was opened, so we must get that file's associated mapping if applicable.
					//IFile.getFullPath.toFile();
					
				}
				
			}

			@Override
			public void partClosed(IWorkbenchPart part) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void partDeactivated(IWorkbenchPart part) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void partOpened(IWorkbenchPart part) {
				// TODO Auto-generated method stub
				
			}
			
		}
				);

	}

	@Override
	public void setFocus() {

	}

}
